package de.szut.bomberman.gui.game;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JLabel;

import de.szut.bomberman.game.logic.DestroyableStone;
import de.szut.bomberman.game.logic.Field;
import de.szut.bomberman.game.logic.PlayerOne;
import de.szut.bomberman.game.logic.PlayerTwo;
import de.szut.bomberman.game.logic.Stone;
import de.szut.bomberman.gui.main.DefaultPanel;
import de.szut.bomberman.gui.mainmenu.MainMenuPanel;
import de.szut.bomberman.main.Controller;
import de.szut.bomberman.main.GameMode;
import de.szut.gui.language.Languages;
import de.szut.gui.language.TranslatableComponent;

public class GamePanel extends DefaultPanel{

	private static final long serialVersionUID = -931846104874336296L;
	private JLabel playerOne;
	private JLabel playerTwo;
	
	private ArrayList<JLabel> stones;
	private ArrayList<JLabel> destroyableStones;

	private ArrayList<TranslatableComponent> translatableComponents;
	
	public GamePanel(GameMode mode) {
		translatableComponents = new ArrayList<TranslatableComponent>();
		
		Languages.prepareComponent("game.GamePanel.return", getReturnButton(), translatableComponents);
		getReturnButton().addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent ev) {
				Controller.getInstance().getGuiController().switchPanel(new MainMenuPanel());
			}
		});
		getHeader().setVisible(false);
		
		Controller.getInstance().getGuiController().getLanguages().translateListOfComponents(translatableComponents);
		
		stones = new ArrayList<JLabel>();
		destroyableStones = new ArrayList<JLabel>();
		
		JLabel field = new JLabel(new ImageIcon(new Field().getTexture()));
		field.setLocation(0, 0);
		field.setSize(650, 650);
		add(field, new Integer(0));
		Stone stone = new Stone();
		DestroyableStone destroyableStone = new DestroyableStone();
		for (int y = 0; y < 13; y++) {
			for (int x = 0; x < 13; x++) {
				if (x % 2 == 0 && y % 2 == 0) {
					JLabel stoneLabel = new JLabel(new ImageIcon(
							stone.getTexture()));
					stoneLabel.setSize(50, 50);
					stoneLabel.setLocation(x * 50, y * 50);
					add(stoneLabel, new Integer(1));
					stones.add(stoneLabel);
					
				} else if(x > 0 && y > 1 && x < 12 && y < 11) {
					JLabel stoneLabel = new JLabel(new ImageIcon(destroyableStone.getTexture()));
					stoneLabel.setSize(50, 50);
					stoneLabel.setLocation(x * 50, y * 50);
					add(stoneLabel, new Integer(1));
					destroyableStones.add(stoneLabel);
				}
			}
		}
		playerOne = new JLabel(new ImageIcon(new PlayerOne().getTexture()));
		playerOne.setLocation(0, 50);
		playerOne.setSize(50, 50);
		add(playerOne, new Integer(1));
		
		playerTwo = new JLabel(new ImageIcon(new PlayerTwo().getTexture()));
		playerTwo.setLocation(600, 550);
		playerTwo.setSize(50, 50);
		add(playerTwo, new Integer(1));
		
		addKeyListener(new Listener(playerOne, playerTwo, stones, destroyableStones, this, mode));
	}
}
